package com.liugh.controller.zhongche;

import com.baomidou.mybatisplus.plugins.Page;
import com.liugh.annotation.CurrentUser;
import com.liugh.base.BusinessException;
import com.liugh.base.PublicResultConstant;
import com.liugh.config.ResponseModel;
import com.liugh.controller.BaseRestController;
import com.liugh.entity.User;
import com.liugh.entity.standard.StandardApprove;
import com.liugh.enums.zhongche.ApproveEnum;
import com.liugh.service.queue.RedisSend;
import com.liugh.service.zhongche.IStandardApproveService;
import com.liugh.service.zhongche.IStandardService;
import com.liugh.util.CommonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@Api(description = "标准审批")
@RequestMapping("/standard_approve")
public class StandardApproveController extends BaseRestController {

    private static Logger Logger = LoggerFactory.getLogger(StandardApproveController.class);

    @Autowired
    IStandardApproveService approveService;

    @Autowired
    IStandardService itemService;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    RedisSend redisSend;


    @ApiOperation("同意申请,管理员操作")
    @PostMapping("/agree")
    public ResponseModel<StandardApprove> handleApprove(@RequestBody StandardApprove approve,
                                                        @CurrentUser User user) throws BusinessException {
        if (approve.getStatus() != ApproveEnum.APPLYING.getCode()) {
            throw new BusinessException(PublicResultConstant.ERROR_APPLY_STATUS);
        }

        approve.setApproveUserNo(user.getUserNo());
        Logger.info("进入redis队列");
        if (redisSend.sendMessage(CommonUtil.toJson(approve))) {
            approve.setAgreeTime(new Date());
            approve.setStatus(ApproveEnum.FUCKING.getCode());  //正在碎片化
            approveService.updateApprove(approve);

            //同步更新item表
            approveService.synUpdateItem(approve.getId(),ApproveEnum.FUCKING.getCode());
            return fillSuccessData(approve);
        }

        //碎片化失败
        approve.setAgreeTime(new Date());
        approve.setStatus(ApproveEnum.FUCK_FAIL.getCode());
        approveService.updateApprove(approve);

        //同步更新item表
        approveService.synUpdateItem(approve.getId(),ApproveEnum.FUCK_FAIL.getCode());
        Logger.error("碎片化失败！");
        return fillSuccessData(approve);
    }

    /**
     * 驳回申请 .
     * @param standardApprove .
     * @return .
     */
    @ApiOperation("驳回申请")
    @PutMapping("/disagree")
    public ResponseModel<StandardApprove> disagreeApprove(@RequestBody StandardApprove standardApprove) throws BusinessException {

        if (approveService.getApproveById(standardApprove.getId()).getStatus() == ApproveEnum.APPLYING.getCode()) {
            standardApprove = approveService.disagreeApprove(standardApprove);
            if (standardApprove != null) {
                return fillSuccessData(standardApprove);
            } else {
                return fillFailData();
            }
        } else {
            throw new BusinessException(PublicResultConstant.ONLY_CAN_DISAGREE_APPLYING);
        }

    }


    /**
     * 撤销申请 .
     * @param id .
     * @return .
     */
    @ApiOperation("撤销申请")
    @DeleteMapping("/cancel")
    public ResponseModel<String> cancelApprove(@RequestParam("id")int id) throws BusinessException {
        if (approveService.getApproveById(id).getStatus() == ApproveEnum.APPLYING.getCode()) {

            approveService.synUpdateItem(id,ApproveEnum.NO_APPROVE.getCode());
            int result = approveService.cancelApprove(id);
            if (result > 0) {
                return fillSuccessData(PublicResultConstant.SUCCEED);
            } else {
                return fillFailData(PublicResultConstant.ERROR);
            }
        } else {
            throw new BusinessException(PublicResultConstant.ONLY_CAN_CANCEL_APPLYING);
        }
    }


    /**
     * 获取我的申请 .
     * @param currentUser .
     * @return .
     */
    @ApiOperation("获取我的所有的申请")
    @GetMapping("/get_my_approve")
    public ResponseModel<Page<StandardApprove>> getMyApprove(@CurrentUser User currentUser,
                                                             @RequestParam(value = "pageNum",defaultValue = "1")int pageNum,
                                                             @RequestParam(value = "pageSize",defaultValue = "10")int pageSize) throws BusinessException {

        Page<StandardApprove> approves = approveService.getMyApprove(new Page<StandardApprove>(pageNum,pageSize),currentUser.getUserNo());
        return fillSuccessDataPage(approves);
    }


    /**
     * 修改申请 .
     * @param standardApprove .
     * @return .
     */
    @ApiOperation("修改申请")
    @PutMapping("/update")
    public ResponseModel<StandardApprove> updateApprove(@RequestBody StandardApprove standardApprove) throws BusinessException {
        if (approveService.getApproveById(standardApprove.getId()).getStatus() == ApproveEnum.APPLYING.getCode()) {
            StandardApprove approve = approveService.updateApprove(standardApprove);
            return fillSuccessData(approve);
        } else {
            throw new BusinessException(PublicResultConstant.ONLY_CAN_UPDATE_APPLYING);
        }

    }


    /**
     * 获取我批准的申请 .
     * @param currentUser .
     * @return .
     */
    @ApiOperation("获取我批准的申请")
    @GetMapping("/get_my_agree_approve")
    public ResponseModel<Page<StandardApprove>> getAgreeApprove(@CurrentUser User currentUser,
                                                             @RequestParam(value = "pageNum",defaultValue = "1")int pageNum,
                                                             @RequestParam(value = "pageSize",defaultValue = "10")int pageSize) throws BusinessException {

        Page<StandardApprove> standardApprovePage = approveService.getAgreeApprove(currentUser, new Page<>(pageNum, pageSize));
        return fillSuccessDataPage(standardApprovePage);
    }


    /**
     * 添加我的申请.
     * @return .
     */
    @ApiOperation("添加申请")
    @PostMapping("/add")
    public ResponseModel<StandardApprove> addApprove(@RequestBody StandardApprove standardApprove,
                                                     @CurrentUser User user) {
        standardApprove =  approveService.addApprove(standardApprove,user);
        return standardApprove != null ? fillSuccessData(standardApprove) : fillFailData();
    }

    /**
     * 获取所有的申请 .
     * @return .
     */
    @ApiOperation("获取所有的申请")
    @GetMapping("/get_all_approve")
    public ResponseModel<Page<StandardApprove>> getAllApprove(@RequestParam(value = "pageNum",defaultValue = "1")int pageNum,
                                                              @RequestParam(value = "pageSize",defaultValue = "10")int pageSize) throws BusinessException {

        Page<StandardApprove> standardApprovePage = approveService.getAllApprove(new Page<>(pageNum, pageSize));
        return fillSuccessDataPage(standardApprovePage);
    }

}
